<?php

namespace app\admin\controller;

use app\helpers\Common;
use app\common\model\Region as RegionMdl;

use support\Request;

class Region extends BaseController
{

    protected $model;

    // 收货地址表
    protected $usersAddressModel;

    public function initialize()
    {
        // 当前model
        //$this->model = 'Region';
    }

    /**
     * 获取地区
     */
    public function getRegion(Request $request)
    {
        $params = $request->all();
        $model = new RegionMdl();

        $parentId = $request->get('p_id');
        $selected = isset($params['selected']) ? $params['selected'] : 0;
        $level = $params['level'];
        $data = $model->where(['parent_id' => $parentId, 'level' => $level])->select();
        $html = '';
        if ($data) {
            foreach ($data as $item) {
                if ($item['id'] == $selected) {
                    $html .= "<option value='{$item['id']}' selected>{$item['name']}</option>";
                } else {
                    $html .= "<option value='{$item['id']}'>{$item['name']}</option>";
                }
            }
        }

        return json($html);
    }

    /**
     * 获取最后一级乡镇
     */
    public function getTown(Request $request)
    {
        $model = new RegionMdl();

        $parentId = $request->get('p_id');
        $data = $model->where('parent_id', $parentId)->select();
        $html = '';
        if ($data) {
            foreach ($data as $item) {
                $html .= "<option value='{$item['id']}'>{$item['name']}</option>";
            }
        }
        if (empty($html)) {
            echo '0';
        } else {
            return json($html);
        }
    }

    /**
     * Ajax获取地址内容
     */
    public function ajaxInitAddress(Request $request)
    {
        if ($request->post()) {
            $model = new RegionMdl();

            $addressId = $request->get('address_id');
            $addressInfo = $this->usersAddressModel->where('address_id', $addressId)->find();

            $province = $model->where(['parent_id' => 0, 'level' => 1])->select();
            $city = $model->where(['parent_id' => $addressInfo['province_id'], 'level' => 2])->find();
            $district = $model->where(['parent_id' => $addressInfo['city_id'], 'level' => 3])->find();
            $town = $model->where(['parent_id' => $addressInfo['district_id'], 'level' => 4])->find();

            $addressList = [
                'address_info' => $addressInfo,
                'province' => $province,
                'city' => $city,
                'district' => $district,
                'town' => $town,
            ];

            return json($addressList, 200);
        }
    }


    // 对应方法二

    /**
     * ajax获取下一级省市
     */
    public function cityId(Request $request)
    {
        if ($request->post()) {
            $model = new RegionMdl();

            $parentId = $request->get('p_id');
            $level = $model->where('id', $parentId)->value('level');
            $level = $level + 1;
            $city = $model->where(['parent_id' => $parentId, 'level' => $level])->select();
            if ($city) {
                // 成功后返回客户端新增的用户ID，并返回提示信息和操作状态
                return json($city, 200);
            } else {
                // 错误后返回错误的操作状态和提示信息
                return json(0, 200);
            }
        }
    }
}